#ifndef _SI24R1_C_
#define _SI24R1_C_
#include "at32f4xx.h"
#include "at32_board.h"

#define READ_REG        0x00  // Define read command to register
#define WRITE_REG       0x20  // Define write command to register
#define RD_RX_PLOAD     0x61  // Define RX payload register address
#define WR_TX_PLOAD     0xA0  // Define TX payload register address
#define FLUSH_TX        0xE1  // Define flush TX register command
#define FLUSH_RX        0xE2  // Define flush RX register command
#define REUSE_TX_PL     0xE3  // Define reuse TX payload register command
#define NOP             0xFF  // Define No Operation, might be used to read status register

//********************************************************************************************************************//
// SPI(SI24R1) registers(addresses)
#define CONFIG          0x00  // 'Config' register address
#define EN_AA           0x01  // 'Enable Auto Acknowledgment' register address
#define EN_RXADDR       0x02  // 'Enabled RX addresses' register address
#define SETUP_AW        0x03  // 'Setup address width' register address
#define SETUP_RETR      0x04  // 'Setup Auto. Retrans' register address
#define RF_CH           0x05  // 'RF channel' register address
#define RF_SETUP        0x06  // 'RF setup' register address
#define STATUS          0x07  // 'Status' register address
#define OBSERVE_TX      0x08  // 'Observe TX' register address
#define RSSI            0x09  // 'Received Signal Strength Indecator' register address
#define RX_ADDR_P0      0x0A  // 'RX address pipe0' register address
#define RX_ADDR_P1      0x0B  // 'RX address pipe1' register address
#define RX_ADDR_P2      0x0C  // 'RX address pipe2' register address
#define RX_ADDR_P3      0x0D  // 'RX address pipe3' register address
#define RX_ADDR_P4      0x0E  // 'RX address pipe4' register address
#define RX_ADDR_P5      0x0F  // 'RX address pipe5' register address
#define TX_ADDR         0x10  // 'TX address' register address
#define RX_PW_P0        0x11  // 'RX payload width, pipe0' register address
#define RX_PW_P1        0x12  // 'RX payload width, pipe1' register address
#define RX_PW_P2        0x13  // 'RX payload width, pipe2' register address
#define RX_PW_P3        0x14  // 'RX payload width, pipe3' register address
#define RX_PW_P4        0x15  // 'RX payload width, pipe4' register address
#define RX_PW_P5        0x16  // 'RX payload width, pipe5' register address
#define FIFO_STATUS     0x17  // 'FIFO Status Register' register address

//********************************************************************************************************************//
// STATUS Register 
#define RX_DR						0x40  /**/
#define TX_DS						0x20
#define MAX_RT					0x10

#define TX_ADR_WIDTH   5  				// 地址宽度
#define TX_PLOAD_WIDTH   32			// 发送的数据长度

#define SI24R1_MOSI	GPIO_Pins_5
#define SI24R1_MISO	GPIO_Pins_4
#define SI24R1_SCK	GPIO_Pins_3
#define SI24R1_CSN	GPIO_Pins_7
#define SI24R1_CE	GPIO_Pins_8
#define SI24R1_IRQ	GPIO_Pins_9

void SI24R1_GPIO_Init(void);

void MySPI_W_CSN(uint8_t BitValue);
void MySPI_W_SCK(uint8_t BitValue);	
void MySPI_W_MOSI(uint8_t BitValue);
uint8_t MySPI_R_MISO(void);
uint8_t MySPI_R_IRQ(void);
void MySPI_W_CE(uint8_t BitValue);

#define	MOSI      (MySPI_W_MOSI)             // Master Out, Slave In pin (output)
#define	MISO      (MySPI_R_MISO())             // Master In, Slave Out pin (input)
#define	SCK       (MySPI_W_SCK)             // Serial Clock pin, (output)
#define	CSN       (MySPI_W_CSN)             // Slave Select pin, (output to CSN)
#define	CE        (MySPI_W_CE)            // Chip Enable pin signal (output)
#define	IRQ       (MySPI_R_IRQ())            // Interrupt signal, from nRF24L01 (input)

void MySPI_Init(void);
void MySPI_Start(void);
void MySPI_Stop(void);

static uint8_t SPI_RW(uint8_t byte);

void SI24R1_Init(void); //SI24R1 Pin Init
uint8_t SI24R1_Write_Reg(uint8_t reg, uint8_t value); 
uint8_t SI24R1_Write_Buf(uint8_t reg, const uint8_t *pBuf, uint8_t bytes);
uint8_t SI24R1_Read_Reg(uint8_t reg);
uint8_t SI24R1_Read_Buf(uint8_t reg, uint8_t *pBuf, uint8_t bytes);

uint8_t tx_package(uint8_t* tx_buf);

void SI24R1_RX_Mode(void);

uint8_t SI24R1_RxPacket(uint8_t *rxbuf);

uint8_t SI24R1_Check(void);

#endif
